#include <iostream>
#include <fstream>
#include "Administrator.h"
using namespace std;

Administrator::Administrator()
{

}

void Administrator::write_account()
{
	int h = 0;
	account ac;
	ofstream outFile;
	outFile.open("account.dat", ios::binary | ios::app);
	h = ac.create_account();
	if (h == 0)outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
	outFile.close();
}

void Administrator::modify_account(int n)
{
	bool found = false;
	account ac;
	fstream File;
	File.open("account.dat", ios::binary | ios::in | ios::out);
	if (!File)
	{
		cout << "\n\n\t\t\tFile could not be open !! Press any Key...";
		return;
	}
	while (!File.eof() && found == false)
	{
		File.read(reinterpret_cast<char *> (&ac), sizeof(account));
		if (ac.retacno() == n)
		{
			ac.show_account();
			cout << "\n\n\t\t\tEnter The New Details of account: " << endl;
			ac.modify();
			int pos = (-1)*static_cast<int>(sizeof(account));
			File.seekp(pos, ios::cur);
			File.write(reinterpret_cast<char *> (&ac), sizeof(account));
			cout << "\n\n\t\t\t\t Record Updated";
			found = true;
			cin.get();
			cin.get();

		}
	}
	File.close();
	if (found == false)
		cout << "\n\n Record Not Found ";
}


void Administrator::delete_account(int n)
{
	account ac;
	ifstream inFile;
	ofstream outFile;
	inFile.open("account.dat", ios::binary);
	if (!inFile)
	{
		cout << "File could not be open !! Press any Key...";
		return;
	}
	outFile.open("Temp.dat", ios::binary);
	inFile.seekg(0, ios::beg);
	while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
	{
		if (ac.retacno() != n)
		{
			outFile.write(reinterpret_cast<char *> (&ac), sizeof(account));
		}
	}
	inFile.close();
	outFile.close();
	remove("account.dat");
	rename("Temp.dat", "account.dat");
	cout << "\n\n\t\t\t\tRecord Deleted ..";
	cin.get();
	cin.get();
}

void Administrator::display_all()
{
	account ac;
	ifstream inFile;
	inFile.open("account.dat", ios::binary);
	if (!inFile)
	{
		cout << "File could not be open !! Press any Key...";
		return;
	}
	cout << "\n\n\t\t\t\t\tACCOUNT HOLDER LIST\n\n\n";
	cout << "\t\t\t====================================================\n";
	cout << "\t\t\tA/c no.       NAME         Type         Balance\n";
	cout << "\t\t\t====================================================\n";
	while (inFile.read(reinterpret_cast<char *> (&ac), sizeof(account)))
	{
		ac.report();
	}
	cin.get();
	cin.get();
	inFile.close();
}


Administrator::~Administrator()
{
}
